﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DiscoveryLogic.DataServices.DataSources;
using DiscoveryLogic.UI.Layouts.Controllers;
using DiscoveryLogic.UI.Layouts.Converters;
using DiscoveryLogic.UI.Layouts.DataAccesses;
using DiscoveryLogic.UI.Layouts.Controls;
using DiscoveryLogic.UI.Layouts.Controls;

namespace DiscoveryLogic.UI.Layouts.ContentScreens
{
    public partial class Heloc : UserControl, IContentScreen
    {
        #region Private Variables

            private readonly Canvas topCanvas;
            private MortgageAccount mortgage;
            private HelocController helocController;
            private double userChatLabelPosition;
            private double userQuestionPosition;
            private double asaChatLabelPosition;
            private double asaGreetingPosition;

        #endregion

        #region Constructors
        
            public Heloc()
            {
                //System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("DiscoveryLogic.UI.Layouts.ContentScreens.Heloc.xaml");
                //topCanvas = (Canvas)this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd());
                InitializeComponent();
                topCanvas = HelocCanvas;

                this.DataContext = new DemoData(this);

                // Put cylinders in
                HelocCylinderAxis helocCylinderAxis = new HelocCylinderAxis();
                helocCylinderAxis.SetValue(Canvas.NameProperty, "CylinderAxis");
                ((Canvas)topCanvas.FindName("PitchStage2_3")).Children.Add(helocCylinderAxis);
                        
                HelocBarChartCylinders helocBarChartCylinders = new HelocBarChartCylinders();
                helocBarChartCylinders.SetValue(Canvas.NameProperty, "Cylinders");
                ((Canvas)topCanvas.FindName("PitchStage2_3")).Children.Add(helocBarChartCylinders);

                // Initialize controller and models
                helocController = new HelocController();
                mortgage = MortgageManager.GetMortgage(1);

                IGraphControl helocGraph = topCanvas.FindName("Cylinders") as IGraphControl;
                IGraphControl drawAmount = topCanvas.FindName("CylinderAxis") as IGraphControl;

                helocController.AddControl(drawAmount);
                helocController.AddControl(helocGraph);
                helocController.SetModel(mortgage);

                ((HelocBarChartCylinders)helocGraph).Model = mortgage;
                ((HelocCylinderAxis)drawAmount).Model = mortgage;

                // Set position for the question to be asked
                TextBlock melabel = topCanvas.FindName("MeChatLabel") as TextBlock;
                userChatLabelPosition = (double)melabel.GetValue(Canvas.TopProperty);

                TextBlock question = topCanvas.FindName("Question") as TextBlock;
                userQuestionPosition = (double)question.GetValue(Canvas.TopProperty);

                // Find and take note of where the ASA greeting text positions are
                TextBlock asaGreetingLabel = topCanvas.FindName("ASA_GreetingLabel") as TextBlock;
                TextBlock asaGreeting = topCanvas.FindName("ASA_Greeting") as TextBlock;

                asaChatLabelPosition = (double)asaGreetingLabel.GetValue(Canvas.TopProperty);
                asaGreetingPosition = (double)asaGreeting.GetValue(Canvas.TopProperty);
            }


        #endregion

        #region Mouse Events

            // Turns the cursor into a hand on mouse enter
            protected void MouseEnter_Hand(object sender, EventArgs e)
            {
                ((FrameworkElement)sender).Cursor = Cursors.Hand;
            }

            // Turns the cursor into an arrow on mouse leave
            protected void MouseLeave_Arrow(object sender, EventArgs e)
            {
                ((FrameworkElement)sender).Cursor = Cursors.Arrow;
            }

            // The X button is clicked
            protected void CloseButton_Click(object sender, EventArgs e)
            {
                topCanvas.Opacity = 0;
                topCanvas.Visibility = Visibility.Collapsed;
                ScreenHideActions();
            }

            // The Show More button is click on the initial HELOC screen
            protected void ShowMore_Click(object sender, EventArgs e)
            {
                Canvas stage1 = topCanvas.FindName("PitchStage1") as Canvas;
                stage1.Opacity = 0;
                stage1.Visibility = Visibility.Collapsed;
                stage1.SetValue(Canvas.ZIndexProperty, 0);

                Canvas stage2 = topCanvas.FindName("PitchStage2_3") as Canvas;
                stage2.Opacity = 1;
                stage2.Visibility = Visibility.Visible;
                stage2.SetValue(Canvas.ZIndexProperty, 1);

                Canvas askWoodgrove = topCanvas.FindName("AskWoodgrovePanel") as Canvas;
                askWoodgrove.Opacity = 1;
                askWoodgrove.Visibility = Visibility.Visible;
                askWoodgrove.SetValue(Canvas.ZIndexProperty, 2);

                Canvas asaIcon = topCanvas.FindName("ASAIcon") as Canvas;
                asaIcon.Opacity = 1;
                asaIcon.Visibility = Visibility.Visible;
                asaIcon.SetValue(Canvas.ZIndexProperty, 2);
            }

            // Ask Woodgrove button is clicked on the 2nd screen of the scenario
            protected void AskASA_Click(object sender, EventArgs e)
            {
                Canvas asaIcon = topCanvas.FindName("ASAIcon") as Canvas;
                asaIcon.Opacity = 0;
                asaIcon.SetValue(Canvas.ZIndexProperty, 0);

                Canvas askWoodgrove = topCanvas.FindName("AskWoodgrovePanel") as Canvas;
                askWoodgrove.Opacity = 0;
                askWoodgrove.SetValue(Canvas.ZIndexProperty, 0);

                Canvas chat = topCanvas.FindName("ASA_ChatBox") as Canvas;
                chat.Opacity = 1;
                chat.Visibility = Visibility.Visible;
                chat.SetValue(Canvas.ZIndexProperty, 2);               
            }

            // Chat box is clicked, we show the question to be asked
            protected void ChatTextBox_Click(object sender, EventArgs e)
            {
                TextBlock question = topCanvas.FindName("Question") as TextBlock;
                Question.Visibility = Visibility.Visible;
                if (question.Opacity == 0)
                {
                    Storyboard sb = topCanvas.FindName("QuestionAppear") as Storyboard;
                    sb.Begin();
                }                
            }

            // Send button is clicked; start storyboards
            protected void SendButton_Click(object sender, EventArgs e)
            {
                TextBlock question = topCanvas.FindName("Question") as TextBlock;
                TextBlock melabel = topCanvas.FindName("MeChatLabel") as TextBlock;

                if (question.Opacity > 0 && melabel.Opacity == 0)
                {
                    Storyboard sb = topCanvas.FindName("QuestionSend") as Storyboard;
                    sb.Completed += new EventHandler(AskQuestionScene2);
                    sb.Begin();                    
                }
            }

            // Call Operator button is clicked
            protected void CallOperator_Click(object sender, EventArgs e)
            {
                Canvas chat = topCanvas.FindName("ASA_ChatBox") as Canvas;
                chat.Opacity = 0;
                chat.Visibility = Visibility.Collapsed;

                Canvas asaIcon = topCanvas.FindName("ASAIcon") as Canvas;
                asaIcon.Opacity = 1;

                TextBlock message = topCanvas.FindName("CallOperatorMessage") as TextBlock;
                message.Opacity = 1;
            }

        #endregion

        #region Helper Methods

            // Event handler for storyboard completed event
            private void AskQuestionScene2(object sender, EventArgs e)
            {
                Storyboard sb = topCanvas.FindName("QuestionToChatWindow") as Storyboard;
                sb.Completed += new EventHandler(AskQuestionScene3);
                sb.Begin();
            }

            // Event handler for storyboard completed event
            private void AskQuestionScene3(object sender, EventArgs e)
            {
                Storyboard sb = topCanvas.FindName("AnswerAppear") as Storyboard;
                sb.Begin();
            }

        #endregion

        #region Public Methods

            // Show the heloc screen
            public void ShowCanvas()
            {
                topCanvas.Opacity = 1;
                topCanvas.Visibility = Visibility.Visible;
            }

            // Reset the screen and screen elements to proper starting positions
            public void ScreenHideActions()
            {
                ((Storyboard)topCanvas.FindName("AnswerAppear")).Stop();
                ((Storyboard)topCanvas.FindName("QuestionToChatWindow")).Stop();
                ((Storyboard)topCanvas.FindName("QuestionSend")).Stop();
                ((Storyboard)topCanvas.FindName("QuestionAppear")).Stop();

                Canvas stage1 = topCanvas.FindName("PitchStage1") as Canvas;
                stage1.Opacity = 1;
                stage1.Visibility = Visibility.Visible;
                stage1.SetValue(Canvas.ZIndexProperty, 1);

                Canvas stage2 = topCanvas.FindName("PitchStage2_3") as Canvas;
                stage2.Opacity = 0;
                stage2.Visibility = Visibility.Collapsed;
                stage2.SetValue(Canvas.ZIndexProperty, 0);

                Canvas askWoodgrove = topCanvas.FindName("AskWoodgrovePanel") as Canvas;
                askWoodgrove.Opacity = 0;
                askWoodgrove.Visibility = Visibility.Collapsed;
                askWoodgrove.SetValue(Canvas.ZIndexProperty, 0);

                Canvas chat = topCanvas.FindName("ASA_ChatBox") as Canvas;
                chat.Opacity = 0;
                chat.Visibility = Visibility.Collapsed;
                chat.SetValue(Canvas.ZIndexProperty, 0);

                Canvas asaIcon = topCanvas.FindName("ASAIcon") as Canvas;
                asaIcon.Opacity = 0;
                asaIcon.Visibility = Visibility.Collapsed;
                asaIcon.SetValue(Canvas.ZIndexProperty, 0);

                TextBlock question = topCanvas.FindName("Question") as TextBlock;
                question.Opacity = 0;
                question.SetValue(Canvas.TopProperty, userQuestionPosition);

                TextBlock melabel = topCanvas.FindName("MeChatLabel") as TextBlock;
                melabel.Opacity = 0;
                melabel.SetValue(Canvas.TopProperty, userChatLabelPosition);

                ((TextBlock)topCanvas.FindName("ASA_GreetingLabel")).SetValue(Canvas.TopProperty, asaChatLabelPosition);

                ((TextBlock)topCanvas.FindName("ASA_Greeting")).SetValue(Canvas.TopProperty, asaGreetingPosition);

                ((TextBlock)topCanvas.FindName("ASA_AnswerLabel")).Opacity = 0;

                ((TextBlock)topCanvas.FindName("ASA_Answer")).Opacity = 0;

                ((TextBlock)topCanvas.FindName("CallOperatorMessage")).Opacity = 0;

                ((HelocCylinderAxis)topCanvas.FindName("CylinderAxis")).ResetSlider();
            }

            // Stub. Not implemented or called yet.
            // Meant for actions before screen is shown.
            public void ScreenShowActions()
            {
            }

        #endregion


            public class DemoData
            {
                private Heloc _parent;
                private double[] HouseValues = new double[] { 585955, 681050, 487200, 563765, 628900, 494900 };

                public DemoData(Heloc parent)
                {
                    _parent = parent;

                    TotalEquity = 82092;
                    UnlockPercentage = 0.8;                    
                }

                public double TotalEquity { get; private set; }

                public double UnlockPercentage { get; private set; }

                public double AvailableEquity { get { return TotalEquity * UnlockPercentage; } }

                public double HouseValues0 { get { return HouseValues[0]; } }
                public double HouseValues1 { get { return HouseValues[1]; } }
                public double HouseValues2 { get { return HouseValues[2]; } }
                public double HouseValues3 { get { return HouseValues[3]; } }
                public double HouseValues4 { get { return HouseValues[4]; } }
                public double HouseValues5 { get { return HouseValues[5]; } }

                public string AvailableEquityMessage
                {
                    get
                    {
                        return string.Format((string)_parent.Resources["Res_UnlockMessage"],
                            UnlockPercentage.ToPercentage(), TotalEquity.ToCurrency(), AvailableEquity.ToCurrency());
                    }
                }
            }
    }


}
